package com.cqcet.wenchuang.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqcet.wenchuang.common.R;
import com.cqcet.wenchuang.entity.Admin;
import com.cqcet.wenchuang.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 商家&管理员登录
 */
@Slf4j
@Api(tags = "商家&管理员账号管理接口")
@RestController
@RequestMapping("/admins")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 商家&管理员登录
     *
     * @param admin
     * @return
     */
    @ApiOperation(value = "商家&管理员登录", notes = "必要:username,password")
    @PostMapping("/login")
    public R<Admin> login(@RequestBody Admin admin) {
        return adminService.login(admin);
    }

    /**
     * 退出登录
     *
     * @return
     */
//    @PreAuthorize("hasAuthority('system:manage:users')")
    @ApiOperation(value = "商家&管理员登出", notes = "无需参数")
    @PostMapping("/logout")
    public R<String> logout() {
        return adminService.logout();
    }

    /**
     * 商家注册方法
     *
     * @param admin
     * @return
     */
    @ApiOperation(value = "商家注册", notes = "必要:username,password,phone,address")
    @PostMapping("/register")
    public R<String> save(@RequestBody Admin admin) {
        adminService.register(admin);
        return R.success("注册成功");
    }

    /**
     * 商家修改信息
     *
     * @param admin
     * @return
     */
    @ApiOperation(value = "商家修改信息", notes = "必要:id,可选:username,password,phone,type,address")
    @PutMapping
    public R<String> update(@RequestBody Admin admin) {
        //不允许修改状态
        admin.setStatus(null);
        adminService.updateById(admin);
        return R.success("修改成功");
    }

    /**
     * 管理员修改状态
     *
     * @param id
     * @param status
     * @return
     */
    @PreAuthorize("hasAuthority('system:manage:merchants')")
    @ApiOperation(value = "修改商家状态", notes = "必要:id,status")
    @GetMapping("/updateStatus/{id}/{status}")
    public R<String> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        LambdaUpdateWrapper<Admin> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ObjectUtils.isNotEmpty(id), Admin::getId, id);
        updateWrapper.set(ObjectUtils.isNotEmpty(status), Admin::getStatus, status);
        adminService.update(updateWrapper);
        return R.success("修改成功");
    }

    /**
     * 商家分页
     *
     * @param page
     * @param pageSize
     * @param username
     * @param status
     * @return
     */
    @ApiOperation(value = "商家管理分页", notes = "管理员使用(无需关心)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页多少", required = true),
            @ApiImplicitParam(name = "username", value = "用户名"),
            @ApiImplicitParam(name = "status", value = "状态", required = true, defaultValue = "1"),
    })
    @GetMapping("/page")
    public R<Page<Admin>> page(Integer page, Integer pageSize, String username, Integer status) {
        Page<Admin> pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        //根据用户名模糊查询
        queryWrapper.like(StringUtils.isNotBlank(username), Admin::getUsername, username);
        //根据状态查询
        queryWrapper.eq(ObjectUtils.isNotEmpty(status), Admin::getStatus, status);

        adminService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }
}
